package com.tanhua.sso.service;


import com.aliyun.oss.OSSClient;
import com.tanhua.sso.config.AliyunConfig;
import com.tanhua.sso.vo.PicUploadResult;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

@Service
public class PicUploadService {

    private static final String[] IMAGE_TYPE = new String[]{".bmp",".jpg",".jpeg",".gif",".png"};

    @Autowired
    private OSSClient ossClient;

    @Autowired
    private AliyunConfig aliyunConfig;

    public PicUploadResult upload(MultipartFile uploadFile){
        PicUploadResult picUploadResult = new PicUploadResult();
        boolean isLegal = false;

        for (String s : IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(),s)){
                isLegal=true;
                break;
            }
        }

        if (!isLegal){
            picUploadResult.setStatus("error");
            return picUploadResult;
        }

        String filename = uploadFile.getOriginalFilename();
        String filePath = getFilePath(filename);

        try {
            ossClient.putObject(aliyunConfig.getBucketName(),filePath,new ByteArrayInputStream(uploadFile.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
            picUploadResult.setStatus("error");
            return picUploadResult;
        }

        picUploadResult.setStatus("done");
        picUploadResult.setName(this.aliyunConfig.getUrlPrefix()+filePath);
        picUploadResult.setUid(String.valueOf(System.currentTimeMillis()));

        return picUploadResult;

    }

    private String getFilePath(String sourceFileName){
        DateTime dateTime = new DateTime();
        return "images/"+dateTime.toString("yyyy")+"/"+dateTime.toString("MM")
                +"/"+dateTime.toString("dd")+"/"+System.currentTimeMillis()
                +RandomUtils.nextInt(100,9999)+"."
                +StringUtils.substringAfterLast((sourceFileName),".");
    }
}